use std::collections::HashMap;
use crate::rest::client::BinanceRestClient;
use crate::errors::*;

impl BinanceRestClient {
    pub async fn create_listen_key(&self) -> Result<String> {
        let res = self.post("/fapi/v1/listenKey", &HashMap::<&str, String>::new(), true).await?;
        let val: serde_json::Value = serde_json::from_str(&res)?;
        Ok(val["listenKey"].as_str().unwrap_or_default().to_string())
    }

    pub async fn keepalive_listen_key(&self, listen_key: &str) -> Result<()> {
        let url = format!("/fapi/v1/listenKey?listenKey={}", listen_key);
        self.put(&url, false).await?;
        Ok(())
    }

    pub async fn delete_listen_key(&self, listen_key: &str) -> Result<()> {
        let url = format!("/fapi/v1/listenKey?listenKey={}", listen_key);
        self.delete(&url, false).await?;
        Ok(())
    }
}
